using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace DynamicFrom
{
    public partial class ConfigSettingsForm : Form
    {
        private TextBox txtClassName;
        private TextBox txtJsonFilePath;
        private Button btnBrowseFile;
        private Button btnSave;
        private Button btnCancel;
        private Label lblClassName;
        private Label lblJsonFilePath;

        public string ClassName { get; private set; }
        public string JsonFilePath { get; private set; }

        public ConfigSettingsForm(string currentClassName = "", string currentJsonFilePath = "")
        {
            InitializeComponent();
            LoadCurrentValues(currentClassName, currentJsonFilePath);
        }

        private void InitializeComponent()
        {
            this.txtClassName = new TextBox();
            this.txtJsonFilePath = new TextBox();
            this.btnBrowseFile = new Button();
            this.btnSave = new Button();
            this.btnCancel = new Button();
            this.lblClassName = new Label();
            this.lblJsonFilePath = new Label();
            this.SuspendLayout();

            // 
            // lblClassName
            // 
            this.lblClassName.AutoSize = true;
            this.lblClassName.Location = new Point(20, 20);
            this.lblClassName.Name = "lblClassName";
            this.lblClassName.Size = new Size(80, 20);
            this.lblClassName.Text = "类名:";
            this.lblClassName.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);

            // 
            // txtClassName
            // 
            this.txtClassName.Location = new Point(20, 45);
            this.txtClassName.Name = "txtClassName";
            this.txtClassName.Size = new Size(450, 25);
            this.txtClassName.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
            this.txtClassName.PlaceholderText = "例如: DynamicFrom.Product, DynamicFrom";

            // 
            // lblJsonFilePath
            // 
            this.lblJsonFilePath.AutoSize = true;
            this.lblJsonFilePath.Location = new Point(20, 85);
            this.lblJsonFilePath.Name = "lblJsonFilePath";
            this.lblJsonFilePath.Size = new Size(100, 20);
            this.lblJsonFilePath.Text = "JSON文件路径:";
            this.lblJsonFilePath.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);

            // 
            // txtJsonFilePath
            // 
            this.txtJsonFilePath.Location = new Point(20, 110);
            this.txtJsonFilePath.Name = "txtJsonFilePath";
            this.txtJsonFilePath.Size = new Size(350, 25);
            this.txtJsonFilePath.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
            this.txtJsonFilePath.ReadOnly = true;

            // 
            // btnBrowseFile
            // 
            this.btnBrowseFile.Location = new Point(380, 110);
            this.btnBrowseFile.Name = "btnBrowseFile";
            this.btnBrowseFile.Size = new Size(90, 25);
            this.btnBrowseFile.Text = "浏览...";
            this.btnBrowseFile.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
            this.btnBrowseFile.Click += new EventHandler(this.BtnBrowseFile_Click);

            // 
            // btnSave
            // 
            this.btnSave.Location = new Point(280, 160);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(90, 30);
            this.btnSave.Text = "保存";
            this.btnSave.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
            this.btnSave.Click += new EventHandler(this.BtnSave_Click);

            // 
            // btnCancel
            // 
            this.btnCancel.Location = new Point(380, 160);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(90, 30);
            this.btnCancel.Text = "取消";
            this.btnCancel.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
            this.btnCancel.Click += new EventHandler(this.BtnCancel_Click);

            // 
            // ConfigSettingsForm
            // 
            this.AutoScaleDimensions = new SizeF(7F, 17F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new Size(500, 210);
            this.Controls.Add(this.lblClassName);
            this.Controls.Add(this.txtClassName);
            this.Controls.Add(this.lblJsonFilePath);
            this.Controls.Add(this.txtJsonFilePath);
            this.Controls.Add(this.btnBrowseFile);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.btnCancel);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ConfigSettingsForm";
            this.StartPosition = FormStartPosition.CenterParent;
            this.Text = "配置设置";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void LoadCurrentValues(string className, string jsonFilePath)
        {
            this.txtClassName.Text = className;
            this.txtJsonFilePath.Text = jsonFilePath;
        }

        private void BtnBrowseFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "JSON文件 (*.json)|*.json|所有文件 (*.*)|*.*";
                openFileDialog.Title = "选择JSON文件";
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.txtJsonFilePath.Text = openFileDialog.FileName;
                }
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtClassName.Text))
            {
                MessageBox.Show("请输入类名", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtClassName.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(this.txtJsonFilePath.Text))
            {
                MessageBox.Show("请选择JSON文件路径", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!File.Exists(this.txtJsonFilePath.Text))
            {
                MessageBox.Show("选择的JSON文件不存在", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            this.ClassName = this.txtClassName.Text.Trim();
            this.JsonFilePath = this.txtJsonFilePath.Text.Trim();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
} 